package com.usian.jyzx.product.service.impl;

import com.usian.jyzx.model.entity.product.Brand;
import com.usian.jyzx.product.mapper.BrandMapper;
import com.usian.jyzx.product.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 07 05 14 05
 **/
@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 查询所有品牌
     *
     *
     *
     * 先取redis查询
     * 没有--》 mysql 查询————。setredis
     * 有--》直接返回
     * @return
     */
    @Override
    public List<Brand> findAll() {
        List brandListRedis = redisTemplate.opsForList().range("brandList", 0, -1);
        log.info("redis--->brandListRedis:{}",brandListRedis);
        if(brandListRedis!=null && brandListRedis.size()>0){

            log.info("redis有值，直接返回{}",brandListRedis);
            return (List<Brand>) brandListRedis;
        }else{

            List<Brand> brandList = brandMapper.findAll();
            log.info("没有值，去查数据库，{}",brandList);
            redisTemplate.opsForList().rightPushAll("brandList",brandList);
            return brandList;
        }


    }

    @Override
    public Brand findById(Long id) {
        return brandMapper.findByBrandId(id);
    }
}
